<?php
	session_start();
	if(!($_SESSION["username"])||($_SESSION["role"]!="TUT")){
		//not logged in or tutor
		header("location:index.php");
	}
	
	$id=$_GET['id'];
	
	// Connect to server and select databse.
	mysql_connect("localhost", "root", "")or die("cannot connect"); 
	mysql_select_db("Project 5.5")or die("cannot select DB");
	
	$sql='SELECT * FROM users WHERE User_ID="'.$id.'"';
	$result=mysql_fetch_row(mysql_query($sql));
	
	
	//updates tutor information with new values
	if(isset($_GET['success'])){
		$sql='UPDATE users SET First_Name="'.$_POST['firstname'].'",Last_Name="'.$_POST['lastname'].'",Username="'.$_POST['username'].'",Password="'.$_POST['password'].'",Email="'.$_POST['email'].'" WHERE User_ID = "'.$id.'"';
		$result=mysql_query($sql);
		$sql='UPDATE classes SET Tutor="0" WHERE Tutor = "'.$id.'"';
		$result=mysql_query($sql);
		$sql='UPDATE classes SET Tutor="'.$id.'" WHERE Class_ID = "'.$_POST['class'].'"';
		$result=mysql_query($sql);
		echo $sql;
		header("location:managetutors.php?message=edit");
	}
	
	//exports classes into a dropdown
	function listClasses(){
				
		$sql="SELECT Class_ID, Location, Day, Time FROM classes";
		$result= mysql_query($sql);
		
		$sql='SELECT Class_ID FROM classes WHERE Tutor="'.$_GET['id'].'"';
		$result2=mysql_fetch_row(mysql_query($sql));
		
		while ($row = mysql_fetch_row($result)){
			$select=" ";
			if ($row[0]==$result2[0]) {
				$select='selected';
			}else{
				$select=" ";
			}
			echo "<OPTION value='".$row[0]."' ".$select.">".$row[2]." ".$row[3]." - ".$row[1]."</OPTION>\n";
		}			
		
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Edit Tutor</title>	
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="js/formsValidate.js"/></script>
</head>
<body>
	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href="management.php">Management</a> \ <a href="managetutors.php">Tutors</a> \ <a href="edittutor.php?id=<?php echo $id ?>">Edit Tutor</a>
		</div>
		<div id="logout"> 
			<a href="login.php?destroy=true">Log Out</a>
		</div>
	</div>
	<a href="edittutor.php?id=<?php echo $id ?>">
		<div id="error">
		</div>
	</a>
	<div style="" id="namebar_edittutor" ></div>
	<div id="body">
	<br/>
		Make your changes and click save to continue.
		<br/><br/>
		<center>
		
		<form action="edittutor.php?id=<?php echo $id ?>&success=true" method="post" name="edittutorform" id="edittutorform" onsubmit="return validateForm('edittutorform');">
			<div style="text-align:left;width:460px;background:#fff;">
			<div style="background-color: #CCC;font-size:25pt;font-weight:bold;text-align:center;"><?php echo "Edit ".$result[1]." ".$result[2]."'s Details" ?></div>
				<br/>
				Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='firstname' value="<?php echo $result[1] ?>"/>
				<input type='text' name='lastname' value="<?php echo $result[2] ?>"/><br/>
				Username:<input type='text' name='username' value="<?php echo $result[3] ?>"/> Password: <input type='text' name='password' value='<?php echo $result[4] ?>'/><br/>
				Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='email' value="<?php echo $result[5] ?>"/> 
				Class:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<SELECT name="class" style="width:155px;">
					<option value="blank"></option>
					<?php
						listClasses();
					?>
				</SELECT>
				<br/>
				<center><br/>
				<input class="subBtn" type='submit' value=''/>
				<a href="managetutors.php">
				<input class="CancelBtn" type="button" name="cancel" value="" />
				</a></center>
			</div>
		</form>
		</center>
		<br/><br/>
	</div>
	
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>